package chenjiuxu.seniorui;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.HashMap;

import chenjiuxu.seniorui.view.MyViewPager;

public class NestedRecyclerviewActivity extends AppCompatActivity {

    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nested_recyclerview);
        viewPager = findViewById(R.id.activity_nested_mvp);
        MyPagerAdapter adapter = new MyPagerAdapter(this);
        viewPager.setAdapter(adapter);

    }


    public class MyPagerAdapter extends PagerAdapter {
        private final Context context;
        private HashMap<Integer, View> mapView;

        public MyPagerAdapter(Context context) {
            this.context = context;
            mapView = new HashMap<>();
        }


        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mapView.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            View view = mapView.get(position);
            if (view == null) view = newView(container, position);
            container.addView(view);
            return mapView.get(position);
        }

        private View newView(ViewGroup viewGroup, int position) {
            RecyclerView view = (RecyclerView) LayoutInflater.from(context).inflate(R.layout.nested_recycler_viewpager_item, viewGroup, false);
            LinearLayoutManager manager = setVerticalManager(view);
            view.setNestedScrollingEnabled(true);


            view.setAdapter(new MyAdapter());

            if (position == 0) {
                view.setBackgroundResource(R.color.colorPrimary);
            } else if (position == 1) {
                view.setBackgroundResource(R.color.mainColor);
            } else {
                view.setBackgroundResource(R.color.colorAccent);
            }
            mapView.put(position, view);
            return view;
        }
    }


    public LinearLayoutManager setVerticalManager(RecyclerView recyclerView) {
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);//线型显示
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//设置滚动方向
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());//设置动画
        return layoutManager;
    }


    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(NestedRecyclerviewActivity.this).inflate(R.layout.item_a, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 100;
        }


        public class MyViewHolder extends RecyclerView.ViewHolder {
            public MyViewHolder(View itemView) {
                super(itemView);
            }
        }

    }

}
